package net.xdclass.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.config.WechatPayConfig;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.manager.ProductOrderManager;
import net.xdclass.service.ProductOrderService;
import net.xdclass.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @description 支付回调
 * @since 2023-09-02 22:06
 */
@Slf4j
@RestController
@RequestMapping("/api/callback/order/v1")
public class PayCallbackController {

    @Autowired
    private WechatPayConfig wechatPayConfig;

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private ProductOrderManager productOrderManager;

    @RequestMapping("/wechat")
    public Map<String, Object> wechatPayCallback(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         * todo 真实的微信支付回调验证流程
         * 获取报文
         * 验证签名
         * 解密AES
         * 处理业务逻辑
         * 相应请求
         */
        /**
         *
         * 回调中需要处理的业务逻辑:
         * 1. 验证订单状态是否支付
         * 2. 如果未支付，则支付
         * 3. 如果已支付，则返回成功告诉微信已收到通知
         */
        log.info("进入业务回调接口:---------");
        String body = CommonUtil.getParamsStringFromBody(request);
        Map dataMap = JSONObject.parseObject(body, Map.class);
        // payResult.put("outTradeNo", eventMessage.getOutTradeNo());
        // payResult.put("mchid", eventMessage.getMchid());
        // payResult.put("orderStatus", eventMessage.getOrderStatus());
        log.info("接收到参数: {}", body);
        log.info("接收到参数: {}", dataMap);
        log.info("接收到参数: {}", dataMap.get("outTradeNo"));
        log.info("接收到参数: {}", dataMap.get("mchid"));
        log.info("接收到参数: {}", dataMap.get("orderStatus"));
        log.info("接收到参数: {}", dataMap.get("attach"));
        String orderStatus = dataMap.get("orderStatus").toString();
        String outTradeNo = dataMap.get("outTradeNo").toString();
        String accountNo = (JSONObject.parseObject( dataMap.get("attach").toString(), Map.class)).get("accountNo").toString();
        // productOrderManager.findByOutTradeNoAndAccountNo(outTradeNo, accountNo)
        // 更新订单状态 改为mq修改
        // int updateOrderRow = productOrderManager.updateOrderPayState(outTradeNo, Long.parseLong(accountNo), orderStatus, "NEW");
        // log.info("更新订单状态成功: {}, 更新条数: {}", body, updateOrderRow);

        // 发送MQ 更改订单状态
        productOrderService.processOrderCallbackMsg(ProductOrderPayTypeEnum.WECHAT_PAY, dataMap);


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        return resultMap;
    }
}
